package com.example.drainage.ui.main

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.augurit.agmobile.agwater5.drainage.common.constant.StringConstant
import com.augurit.agmobile.agwater5.drainage.supervise.model.SuperviseRole
import com.augurit.agmobile.agwater5.drainage.supervise.model.SuperviseType
import com.augurit.agmobile.agwater5.drainage.supervise.view.SuperviseSignActivity
import com.augurit.agmobile.agwater5.drainage.supervise.view.SuperviseSignDetailActivity
import com.augurit.agmobile.agwater5.drainage.supervise.view.SuperviseVariousActivity
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.arm.base.BaseActivity
import com.example.arm.ext.dpInt
import com.example.drainage.databinding.ActivityDrainageMainBinding
import com.example.drainage.ui.calendar.CalendarActivity
import com.example.drainage.ui.certupload.CertSupervisionUploadActivity
import com.example.drainage.ui.downwin.DownWinActivity
import com.example.drainage.ui.media.AudioPlayerActivity
import com.example.drainage.ui.multipletype.MultipleTypeRecyclerViewActivity
import com.example.drainage.ui.realmdao.RealmDaoActivity
import com.example.drainage.ui.shadow.ShadowActivity
import com.example.drainage.ui.surface.SurfaceActivity

class DrainageMainActivity : BaseActivity<ActivityDrainageMainBinding>() {

    private lateinit var adapter: DrainageMainAdapter

    override fun initView(savedInstanceState: Bundle?): ActivityDrainageMainBinding {
        return ActivityDrainageMainBinding.inflate(layoutInflater)
    }

    override fun initData(savedInstanceState: Bundle?) {
        binding.apply {
            adapter = DrainageMainAdapter(object : DiffUtil.ItemCallback<DrainageMainBean>() {
                override fun areItemsTheSame(oldItem: DrainageMainBean, newItem: DrainageMainBean): Boolean {
                    return oldItem.title == newItem.title
                }

                override fun areContentsTheSame(oldItem: DrainageMainBean, newItem: DrainageMainBean): Boolean {
                    return oldItem == newItem
                }
            })
            adapter.submitList(
                listOf(
                    DrainageMainBean("Calendar", CalendarActivity::class.java),
                    DrainageMainBean("CertSupervisionUpload", CertSupervisionUploadActivity::class.java),
                    DrainageMainBean("DownWin", DownWinActivity::class.java),
                    DrainageMainBean("MultipleTypeRecyclerView", MultipleTypeRecyclerViewActivity::class.java),
                    DrainageMainBean("Shadow", ShadowActivity::class.java),
                    DrainageMainBean("SurfaceActivity", SurfaceActivity::class.java),
                    DrainageMainBean("SuperviseVariousActivity", SuperviseVariousActivity::class.java) {
                        putExtra(StringConstant.KEY_TYPE, SuperviseType.Reservoir)
                        putExtra(StringConstant.KEY_ID, "112233")
                    },
                    DrainageMainBean("SuperviseSignActivity", SuperviseSignActivity::class.java) {
                        putExtra(StringConstant.KEY_TYPE, SuperviseType.Reservoir)
                        putExtra(StringConstant.KEY_ID, "112233")
                        putExtra(StringConstant.KEY_PERSON, SuperviseRole.TeamMembers)
                    },
                    DrainageMainBean("SuperviseSignActivity22", SuperviseSignActivity::class.java) {
                        putExtra(StringConstant.KEY_TYPE, SuperviseType.Reservoir)
                        putExtra(StringConstant.KEY_ID, "112233")
                        putExtra(StringConstant.KEY_PERSON, SuperviseRole.GroupLeader)
                    },
                    DrainageMainBean("SuperviseSignDetailActivity", SuperviseSignDetailActivity::class.java),
                    DrainageMainBean("RealmDaoActivity", RealmDaoActivity::class.java),
                    DrainageMainBean("AudioPlayerActivity", AudioPlayerActivity::class.java),
                )
            )
            adapter.onClickListener = { bean ->
                startActivity(Intent(this@DrainageMainActivity, bean.clazz).also {
                    bean.intent?.invoke(it)
                })
            }

            recyclerView.adapter = adapter
        }
    }

    data class DrainageMainBean(val title: String, val clazz: Class<out Activity>, val intent: (Intent.() -> Unit)? = null)

    class DrainageMainAdapter(config: DiffUtil.ItemCallback<DrainageMainBean>) : ListAdapter<DrainageMainBean, BaseViewHolder>(config) {

        var onClickListener: ((DrainageMainBean) -> Unit)? = null

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
            val textView = TextView(parent.context)
            textView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 50.dpInt)
            textView.gravity = Gravity.CENTER_VERTICAL
            textView.textSize = 16f
            return BaseViewHolder(textView)
        }

        override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
            (holder.itemView as TextView).text = getItem(position).title
            holder.itemView.setOnClickListener {
                onClickListener?.invoke(getItem(holder.absoluteAdapterPosition))
            }
        }
    }
}